﻿using System;
using System.Web;
using System.Web.DynamicData;
using System.Web.Routing;
using PD.DataAccess;

namespace PD.Web
{
    /// <summary>
    /// The methods, properties, and events that are common to all application objects in an ASP.NET application.
    /// </summary>
    public class Global : System.Web.HttpApplication
    {
        /// <summary>
        /// Represents one or multiple databases that are used by ASP.NET Dynamic Data.
        /// </summary>
        private static MetaModel defaultModel = new MetaModel();

        /// <summary>
        /// Gets one or multiple databases that are used by ASP.NET Dynamic Data.
        /// </summary>
        public static MetaModel DefaultModel
        {
            get
            {
                return defaultModel;
            }
        }

        /// <summary>
        /// Registers the routes.
        /// </summary>
        /// <param name="routes">The routes.</param>
        public static void RegisterRoutes(RouteCollection routes)
        {
            Global.DefaultModel.RegisterContext(typeof(PDDataClassesDataContext), new ContextConfiguration() { ScaffoldAllTables = true });

            // The following statement supports separate-page mode, where the List, Detail, Insert, and 
            // Update tasks are performed by using separate pages. To enable this mode, uncomment the following 
            // route definition, and comment out the route definitions in the combined-page mode section that follows.
            routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
            {
                Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert" }),
                Model = DefaultModel
            });

            // The following statements support combined-page mode, where the List, Detail, Insert, and
            // Update tasks are performed by using the same page. To enable this mode, uncomment the
            // following routes and comment out the route definition in the separate-page mode section above.
            ////routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")
            ////{
            ////    Action = PageAction.List,
            ////    ViewName = "ListDetails",
            ////    Model = DefaultModel
            ////});

            ////routes.Add(new DynamicDataRoute("{table}/ListDetails.aspx")
            ////{
            ////    Action = PageAction.Details,
            ////    ViewName = "ListDetails",
            ////    Model = DefaultModel
            ////});
        }

        /// <summary>
        /// Handles the Start event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Application_Start(object sender, EventArgs e)
        {
            Global.RegisterRoutes(RouteTable.Routes);
            this.Application[PDConstants.WorkflowRuntimeInstanceKey] = PDWorkflowRuntimeUtility.Configure();
            Logger.Instance.Info("Application started.");
        }

        /// <summary>
        /// Handles the End event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Application_End(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Handles the Error event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Application_Error(object sender, EventArgs e)
        {
            Exception exception = HttpContext.Current.Server.GetLastError();
            Logger.Instance.Error("An error has occurred.", exception.InnerException);
        }

        /// <summary>
        /// Handles the Start event of the Session control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Session_Start(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Handles the End event of the Session control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Session_End(object sender, EventArgs e)
        {
        }
    }
}
